//
//  Qihaolou.swift
//  UUVideo
//
//  Created by Galaxy on 2023/1/13.
//  Copyright © 2023 qykj. All rights reserved.
//

import UIKit
import Ji

class Qihaolou: WebsiteBaseModel {
    override init() {
        super.init()
        webUrlStr = "https://m.dro4.co/"
        websiteName = "dro4"
        valueArr = ["video"]
    }
    override func getIndexData() -> [ListModel] {
        let jiDoc = Ji.init(htmlURL: URL.init(string: webUrlStr)!)
        if jiDoc == nil {
            return []
        }
        var resultArr: [ListModel] = []
        let listModel = ListModel.init()
        let titleXpath = "//div[@id='list_videos_most_recent_videos_items']/div/a/div[2]/div[2]"
        let urlXpath = "//div[@id='list_videos_most_recent_videos_items']/div/a/@href"
        let imgXpath = "//div[@id='list_videos_most_recent_videos_items']/div/a/div/img/@src"
        let titleNodeArr = jiDoc?.xPath(titleXpath)
        let urlNodeArr = jiDoc?.xPath(urlXpath)
        let imgNodeArr = jiDoc?.xPath(imgXpath)
        listModel.title = "video"
        listModel.more = true
        listModel.list = []
        for (i, _) in titleNodeArr!.enumerated() {
            var videoModel = VideoModel.init()
            videoModel.name = titleNodeArr![i].content!
            videoModel.webType = websiteType.qihaolou.rawValue
            let detailUrl: String = urlNodeArr![i].content!
            videoModel.detailUrl = Tool.checkUrl(urlStr: detailUrl, domainUrlStr: webUrlStr)
            let picUrl: String = imgNodeArr![i].content!
            videoModel.picUrl = Tool.checkUrl(urlStr: picUrl, domainUrlStr: webUrlStr)
            videoModel.num = ""
            videoModel.type = 3
            listModel.list.append(videoModel)
        }
        resultArr.append(listModel)
        return resultArr
    }
    override func getVideoList(videoTypeIndex: Int, category: (area: String, year: String, videoCategory: String), pageNum: Int) -> [ListModel] {
        let videoType = valueArr[videoTypeIndex]
        let urlStr = webUrlStr + "?mode=async&function=get_block&block_id=list_videos_most_recent_videos&sort_by=rating_week&from=\(pageNum)"
        let newUrlStr = urlStr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
        let jiDoc = Ji(htmlURL: URL.init(string: newUrlStr)!)
        if jiDoc == nil {
            return []
        }
        let listModel = ListModel.init()
        listModel.title = ""
        listModel.more = false
        listModel.list = []
        let titleXpath = "//div[@id='list_videos_most_recent_videos_items']/div/a/div[2]/div[2]"
        let urlXpath = "//div[@id='list_videos_most_recent_videos_items']/div/a/@href"
        let imgXpath = "//div[@id='list_videos_most_recent_videos_items']/div/a/div/img/@src"
        let titleNodeArr = jiDoc?.xPath(titleXpath)
        let urlNodeArr = jiDoc?.xPath(urlXpath)
        let imgNodeArr = jiDoc?.xPath(imgXpath)
        for (i, _) in titleNodeArr!.enumerated() {
            var videoModel = VideoModel.init()
            videoModel.name = titleNodeArr![i].content!
            videoModel.num = ""
            let detailUrl: String = urlNodeArr![i].content!
            videoModel.detailUrl = Tool.checkUrl(urlStr: detailUrl, domainUrlStr: webUrlStr)
            let picUrl: String = imgNodeArr![i].content!
            videoModel.picUrl = Tool.checkUrl(urlStr: picUrl, domainUrlStr: webUrlStr)
            videoModel.type = 3
            videoModel.webType = websiteType.qihaolou.rawValue
            listModel.list.append(videoModel)
        }
        return [listModel]
    }
    override func getVideoCategory(videoTypeIndex: Int) -> [CategoryListModel] {
        []
    }
    override func getVideoDetail(urlStr: String) -> (result: Bool, model: VideoModel) {
        let jiDoc = Ji(htmlURL: URL.init(string: urlStr)!)
        if jiDoc == nil {
            return (result: false, model: VideoModel.init())
        }
        let baseUrl = Tool.getRegularData(regularExpress: "((http://)|(https://))[^\\.]*\\.(?<domain>[^/|?]*)", content: urlStr)[0]
        var videoModel = VideoModel.init()
        videoModel.detailUrl = urlStr
        videoModel.videoArr = []
        videoModel.tagArr = []
        videoModel.serialArr = []
        videoModel.picUrl = ""
        //        剧集
        // 获取线路
        let model = CircuitModel.init()
        model.name = "默认线路"
        let serialModel = SerialModel.init()
        serialModel.name = "默认"
        serialModel.detailUrl = urlStr
        model.serialArr.append(serialModel)
        let circuitArr:[CircuitModel] = [model]
        videoModel.circuitArr = circuitArr
        videoModel.serialNum = videoModel.serialArr.count
        //        推荐视频
        let titleXpath = "//div[@id='list_videos_related_videos_items']/div/a/div[2]/div[2]"
        let urlXpath = "//div[@id='list_videos_related_videos_items']/div/a/@href"
        let imgXpath = "//div[@id='list_videos_related_videos_items']/div/a/div/img/@src"
        let titleNodeArr = jiDoc?.xPath(titleXpath)
        let urlNodeArr = jiDoc?.xPath(urlXpath)
        let imgNodeArr = jiDoc?.xPath(imgXpath)
        if titleNodeArr!.count > 0 {
            for (index, titleNode) in titleNodeArr!.enumerated() {
                var model = VideoModel.init()
                model.name = titleNode.content!
                model.name = model.name.replacingOccurrences(of: "\n", with: "")
                model.name = model.name.replacingOccurrences(of: "\t", with: "")
                let imgPic: String = imgNodeArr![index].content!
                model.picUrl = Tool.checkUrl(urlStr: imgPic, domainUrlStr: baseUrl)
                let recommandUrlStr: String = urlNodeArr![index].content!
                model.detailUrl = Tool.checkUrl(urlStr: recommandUrlStr, domainUrlStr: baseUrl)
                model.webType = websiteType.qihaolou.rawValue
                model.num = ""
                model.type = 3
                videoModel.videoArr.append(model)
            }
        }
        return (result: true, model: videoModel)
    }
    override func getVideoPlayerDetail(urlStr: String) -> (result: Bool, model: VideoModel) {
        let jiDoc = Ji(htmlURL: URL.init(string: urlStr)!)
        if jiDoc == nil {
            return (result: false, model: VideoModel.init())
        } else {
            let baseUrl = Tool.getRegularData(regularExpress: "((http://)|(https://))[^\\.]*\\.(?<domain>[^/|?]*)", content: urlStr)[0]
            var videoModel = VideoModel.init()
            videoModel.videoArr = []
            videoModel.serialArr = []
            // 播放地址
            let playerXpath = "//video/source/@src"
            let playerNodeArr = jiDoc?.xPath(playerXpath)
            if playerNodeArr!.count>0 {
                videoModel.videoUrl = playerNodeArr![0].content!
            }else{
                return (result: true, model: videoModel)
            }
            // 获取线路
            let model = CircuitModel.init()
            model.name = "默认线路"
            let serialModel = SerialModel.init()
            serialModel.name = "默认"
            serialModel.detailUrl = urlStr
            model.serialArr.append(serialModel)
            let circuitArr:[CircuitModel] = [model]
            videoModel.circuitArr = circuitArr
            videoModel.serialNum = videoModel.serialArr.count
            // 获取推荐视频
            let recommendtitleXpath = "//div[@id='list_videos_related_videos_items']/div/a/div[2]/div[2]"
            let recommendurlXpath = "//div[@id='list_videos_related_videos_items']/div/a/@href"
            let recommendimgXpath = "//div[@id='list_videos_related_videos_items']/div/a/div/img/@src"
            let recommendTitleNodeArr = jiDoc?.xPath(recommendtitleXpath)
            let recommendUrlNodeArr = jiDoc?.xPath(recommendurlXpath)
            let recommendImgNodeArr = jiDoc?.xPath(recommendimgXpath)
            if recommendTitleNodeArr!.count > 0 {
                for (index, item) in recommendTitleNodeArr!.enumerated() {
                    var model = VideoModel.init()
                    model.name = item.content!
                    model.name = model.name.replacingOccurrences(of: "\n", with: "")
                    model.name = model.name.replacingOccurrences(of: "\t", with: "")
                    let imgPic: String = recommendImgNodeArr![index].content!
                    model.picUrl = Tool.checkUrl(urlStr: imgPic, domainUrlStr: baseUrl)
                    let recommandUrlStr: String = recommendUrlNodeArr![index].content!
                    model.detailUrl = Tool.checkUrl(urlStr: recommandUrlStr, domainUrlStr: baseUrl)
                    model.num = ""
                    model.type = 3
                    model.webType = websiteType.qihaolou.rawValue
                    videoModel.videoArr.append(model)
                }
            }
            videoModel.videoUrl = videoModel.videoUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
            return (result: true, model: videoModel)
        }
    }
    override func getSearchData(pageNum: Int, keyword: String) -> [ListModel] {
        return []
    }
}
